package com.mcflies.ui.pullDownToRefresh;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Window;

import com.mcflies.commons.PullDownToRefreshAdapter;
import com.mcflies.commons.PullDownToRefreshListView;
import com.mcflies.commons.PullDownToRefreshListView.OnRefreshListener;
import com.mcflies.ui.R;

/**
 * @author:mcfly_jun
 * @create_date:2012-8-14
 * @comment:下拉更新学习
 */
public class PullDownToRefreshActivity extends Activity {
	List<String> dataList = new ArrayList<String>();
	private PullDownToRefreshListView pullDownToRefresh_LV;
	private PullDownToRefreshAdapter pullDownToRefreshAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pull_down_to_refresh_main);
		initWidget();
		initData();
		pullDownToRefreshAdapter = new PullDownToRefreshAdapter(this, dataList);
		pullDownToRefresh_LV.setAdapter(pullDownToRefreshAdapter);

		pullDownToRefresh_LV.setonRefreshListener(new OnRefreshListener() {
			public void onRefresh() {
				new AsyncTask<Void, Void, Void>() {
					protected Void doInBackground(Void... params) {
						try {
							Thread.sleep(1000);
						} catch (Exception e) {
							e.printStackTrace();
						}
						dataList.add(0, "刷新后的内容");
						return null;
					}

					@Override
					protected void onPostExecute(Void result) {
						pullDownToRefreshAdapter.notifyDataSetChanged();
						pullDownToRefresh_LV.onRefreshComplete();
					}
				}.execute(null);
			}
		});
	}

	private void initWidget() {
		pullDownToRefresh_LV = (PullDownToRefreshListView) findViewById(R.id.pullDownToRefresh_LV);
	}

	private void initData() {
		for (int i = 0; i < 20; i++) {
			dataList.add("下拉刷新数据" + i);
		}
	}
}
